package com.zhangkunhao.notes.web.servlet;

import com.zhangkunhao.notes.utils.CheckCodeUtil;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;

import java.io.IOException;

/**
 * 生成图片验证码的controller
 *
 * @author 20642
 */
@WebServlet("/checkCodeServlet")
public class CheckCodeServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
        //存到session,要在response.getOutputStream()之前创建
        HttpSession session = request.getSession();

        //生成验证码
        ServletOutputStream os = response.getOutputStream();
        String checkCode = CheckCodeUtil.outputVerifyImage(100, 50, os, 4);
        System.out.println("图片的验证码是：" + checkCode);

        //存到session
        session.setAttribute("checkCodeGenerate", checkCode);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request, response);
    }
}
